// Copyright (c) 2007 Robin Boerdijk. All rights reserved.
//
// This software is licensed under the terms of the Microsoft Reciprocal License as described 
// in the file LICENSE.txt, which you should have received as part of this distribution. The 
// license terms are also available at http://www.opensource.org/licenses/ms-rl.html.

#include "wtkpc.h"

#include "wtk/EditControl.h"
#include "wtk/Clipboard.h"

namespace wtk
{
	EditControl::EditControl(DialogWindow *dialogWindow)
		: Control(L"EDIT", dialogWindow)
	{
		this->style |= WS_BORDER;
	}

	LRESULT EditControl::ProcessMessage(HWND windowHandle, UINT message, WPARAM wParam, LPARAM lParam)
	{
		switch (message)
		{
		case WM_CHAR:
			if (AcceptChar(wchar_t(wParam))) {
				return Control::ProcessMessage(windowHandle, message, wParam, lParam);
			} else {
				return 0;
			}
		case WM_PASTE:
			return ProcessPasteMessage();
		default:
			return Control::ProcessMessage(windowHandle, message, wParam, lParam);
		}
	}

	LRESULT EditControl::ProcessPasteMessage()
	{
		std::wstring text;
		if (Clipboard(this).GetText(text)) {
			Paste(text);
		}
		return 0;
	}

	bool EditControl::AcceptChar(wchar_t ch)
	{
		return true;
	}

	void EditControl::Paste(const std::wstring &text)
	{
		win32::SendMessageW<void>(this->handle, EM_REPLACESEL, TRUE, text.c_str());
	}
}
